﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace LiteFramework
{
    public class ByteHelper
    {
        private ByteHelper()
        {
        }

        public static bool CompareByteArray(byte[] buffer1, byte[] buffer2)
        {
            if (buffer1.Length != buffer2.Length)
            {
                return false;
            }
            for (int i = 0; i < buffer1.Length; i++)
            {
                if (buffer1[i] != buffer2[i])
                {
                    return false;
                }
            }
            return true;
        }

        public static object Deserialize(byte[] buffer)
        {
            object obj2 = null;
            if ((buffer == null) || (buffer.Length == 0))
            {
                return obj2;
            }
            using (MemoryStream stream = new MemoryStream(buffer))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                return formatter.Deserialize(stream);
            }
        }

        public static byte[] Serialize(object obj)
        {
            byte[] buffer = null;
            if (obj == null)
            {
                return buffer;
            }
            using (MemoryStream stream = new MemoryStream())
            {
                new BinaryFormatter().Serialize(stream, obj);
                stream.Position = 0L;
                return stream.ToArray();
            }
        }
    }
}
